package com.sykj.manage.web;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.sykj.common.MYSConstant;
import com.sykj.manage.service.CustomUserService;
import com.sykj.model.CustomUser;
import com.sykj.model.ShopCustom;
import com.sykj.util.ManageBody;

@Controller
@RequestMapping("/customuser")
public class CustomUserController {
	
	private static final String UPLOAD_PATH = "/static/manage/images/";
	@Autowired
	CustomUserService customUserService;
	
	@RequestMapping("/custom")
	@ResponseBody
	public ManageBody index(Model mav,CustomUser custom ) {
		List<CustomUser> cu = customUserService.getAll(custom);
		return ManageBody.getData(cu.size(), cu, "");
	}
	//商铺名称下拉框显示
	@RequestMapping("/shopname")
	@ResponseBody
	public ManageBody getshopname(CustomUser custom){
		List<CustomUser> getshopnames = customUserService.getshop(custom);
		System.out.println("===============");
		return ManageBody.getData(getshopnames.size(), getshopnames, "");
	}
	//最近到店时间下拉框显示
	@RequestMapping("/recenttime")
	@ResponseBody
	public ManageBody getrecenttime(CustomUser custom) {
		List<CustomUser> getrecenttime = customUserService.getbsc(custom);
		System.out.println("================");
		return ManageBody.getData(getrecenttime.size(), getrecenttime, "");
	}
	//微信状态,注册状态下拉框显示
	@RequestMapping("/enrollmode")
	@ResponseBody
	public ManageBody getenrollmode(CustomUser custom) {
		List<CustomUser> getenrollmode = customUserService.getcu(custom);
		System.out.println("================");
		return ManageBody.getData(getenrollmode.size(), getenrollmode, "");
	}
	
	//删除
	@RequestMapping("/del/{custom_user_id}")
	public String del(@PathVariable("custom_user_id") String custom_user_id, Model model) {
		//根据id获取所选对象数据
		int cnt = customUserService.del(custom_user_id);
		System.out.println("成功删除" + cnt+"条记录");
		return "redirect:/customuser/";
	}
	
	//添加
	@RequestMapping("/add")
	public String add(CustomUser customuser,@RequestParam("file")MultipartFile file,HttpServletRequest request,ShopCustom shopcustom){
		String realPath = request.getSession().getServletContext().getRealPath(UPLOAD_PATH);
		//设置给前台内容的数据格式
		System.out.println("customuser"+customuser);

		System.out.println("realPath"+realPath);
		String filename = file.getOriginalFilename();
		System.out.println("文件名称："+filename);
		File taFile = new File(realPath+"/"+filename);
		if(!taFile.exists()){
			taFile.mkdirs();
		}
		try {
			file.transferTo(taFile);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String fileurl = request.getContextPath()+UPLOAD_PATH+filename;
		System.out.println("文件URL"+fileurl);
		customuser.setPhoto(fileurl);//存图片路径
		
		
		//第一个是转时间，第二个是id自增长
		customuser.setBorn_date(new Date());
		shopcustom.setShop_id("10000001");
		shopcustom.setCreate_time(new Date());
		shopcustom.setRecent_time(new Date());
//		bisCustomUser.setCustom_user_id(IDGenerator.generatorId());
		customUserService.add(customuser);
		return "redirect:/manage/customuser.html";
		
	}
	//添加
//	@RequestMapping("/Addcust")
//	@ResponseBody
//	public String add(CustomUser customuser,@RequestParam(value="file",required=false) MultipartFile[] file
//				, HttpServletRequest request)
//			throws IOException {
//		System.out.println("======="+customuser);
//	    int count=1;
//	    for (MultipartFile mf : file) {
//	        if(!mf.isEmpty()){
//	            // 使用UUID给图片重命名，并去掉四个“-”
//	            String name = UUID.randomUUID().toString().replaceAll("-", "");
//	            // 获取文件的扩展名
//	            String ext = FilenameUtils.getExtension(mf.getOriginalFilename());  
//	            // 设置图片上传路径
//	            String url = request.getSession().getServletContext().getRealPath("/");
//	            System.out.println(url);
//	            //把图片上传路径的常量调用过来
//	            String fullPath =  MYSConstant.UPLOAD_MANAGE_PROJECT + name + "." + ext;
//	            File file1 = new File(url + fullPath);
//	            if (!file1.exists()) {
//	            	file1.createNewFile();
//	            }
//	            // 以绝对路径保存重名命后的图片
//	            mf.transferTo(file1);
//	            // 把图片存储路径保存到数据库
//	            if(count==1){
//	            	customuser.setPhoto(fullPath);
//	            }
//	        }  
//	        count++;
//	    }
//	    System.out.println("]==="+customuser);
//		int addPro =customUserService.add(customuser);
//		if (addPro == 1) {
//			System.out.println("成功");
//		} else {
//			System.out.println("失败");
//		}
//		return "redirect:/manage/customuser.html";
//	}
	
	//修改
	@RequestMapping("/update/{custom_user_id}")
	@ResponseBody
	public Object update(@PathVariable("custom_user_id") String custom_user_id ,Model model) {
		CustomUser findAllById = customUserService.findAllById(custom_user_id);
		return findAllById;
	}
	@RequestMapping("/up")
	public String up(CustomUser customuser,@RequestParam("file")MultipartFile file,HttpServletRequest request,ShopCustom shopcustom){
		String realPath = request.getSession().getServletContext().getRealPath(UPLOAD_PATH);
		//设置给前台内容的数据格式
		System.out.println("customuser"+customuser);

		System.out.println("realPath"+realPath);
		String filename = file.getOriginalFilename();
		System.out.println("文件名称："+filename);
		File taFile = new File(realPath+"/"+filename);
		if(!taFile.exists()){
			taFile.mkdirs();
		}
		try {
			file.transferTo(taFile);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String fileurl = request.getContextPath()+UPLOAD_PATH+filename;
		System.out.println("文件URL"+fileurl);
		customuser.setPhoto(fileurl);//存图片路径
		
		
		//第一个是转时间，第二个是id自增长
		customuser.setBorn_date(new Date());
		shopcustom.setShop_id("10000001");
		shopcustom.setCreate_time(new Date());
		shopcustom.setRecent_time(new Date());
//		bisCustomUser.setCustom_user_id(IDGenerator.generatorId());
		customUserService.update(customuser);
		return "redirect:/manage/customuser.html";
		
	}
	
	//id查看基本详情
	@RequestMapping("/findCustomerById/{custom_user_id}")
	@ResponseBody
	public Object info(@PathVariable("custom_user_id") String custom_user_id){
		CustomUser findCustomerById = customUserService.findAllById(custom_user_id);
		System.err.println("=======1111111111111======="+findCustomerById);
		return findCustomerById;
	}
}
